﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Brain
{
    /// <summary>
    /// Class responsible for Serializing / Deserializing in Binary
    /// </summary>
    public static class BinaryWRiter
    {
        /// <summary>
        /// Serializes a List of Data into a Binary file
        /// </summary>
        /// <param name="Data">The Data list to serialize</param>
        public static void Write(List<SerializationData> Data, Stream stream)
        {
            // Craetes a new Binary Writer from the stream
            BinaryWriter writer = new BinaryWriter(stream);

            // The beginning of the file will show it was
            // made with the STAIRS game engine
            writer.Write(new char[] { 'B', 'R', 'A', 'I', 'N' });

            // writes how much lists we have
            writer.Write(Data.Count);

            for (int i = 0; i < Data.Count; i++)
            {
                // for every list
                SerializationData list = Data[i];

                // write the assembly name (so we know what library to load)
                //writer.Write(list.AssemblyFullName);
                // write the Type of the object that used this data, so we 
                // can instantitate it later
                //writer.Write(list.ParentType);

                // write the list list's count
                writer.Write(list.Data.Count);
                for (int x = 0; x < list.Data.Count; x++)
                {
                    SerializationDataObject obj = list.Data[x];

                    writer.Write(obj.Key);
                    if (obj.Object != null)
                    {
                        writer.Write(obj.Object.ToString());
                    }
                    else
                    {
                        writer.Write("null");
                    }
                }
            }

            writer.Flush();
            writer.Dispose();
        }

        /// <summary>
        /// Deserializes the Binary file from the stream
        /// </summary>
        /// <param name="Manager">The Serialization Manager deserializing</param>
        /// <param name="stream">Stream that contains the binary file.</param>
        /// <param name="DataList">The List to deserialize to</param>
        public static List<SerializationData> Read(Stream stream)
        {
            List<SerializationData> DataList = new List<SerializationData>();
            BinaryReader reader = new BinaryReader(stream);

            char[] buffer = new char[5];
            reader.Read(buffer, 0, 5);

            // Look for the start STAIRS chars
            if (buffer[0] != 'B' || buffer[1] != 'R' ||
                buffer[2] != 'A' || buffer[3] != 'I' ||
                buffer[4] != 'N')
            {
                throw new Exception("Serialization file was not serialized by Brain Engine");
            }

            int count = reader.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                SerializationData list = new SerializationData();
                //list.AssemblyFullName = reader.ReadString();
                //list.ParentType = reader.ReadString();
                DataList.Add(list);

                int listCount = reader.ReadInt32();
                for (int k = 0; k < listCount; k++)
                {
                    list.AddData(reader.ReadString(), reader.ReadString());
                }
            }

            reader.Dispose();

            return DataList;

        }
    }
}
